package com.ciwong.epaper.modules.homework.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.ciwong.ciwongwrite.utils.CWUtils;
import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.homework.bean.ClassWorkContent;
import com.ciwong.epaper.modules.homework.bean.ClassWorkRecord;
import com.ciwong.epaper.modules.homework.bean.PublishWorkRecords;
import com.ciwong.epaper.util.HttpRequest;
import com.ciwong.epaper.widget.NoScrollListView;
import com.ciwong.libs.utils.volley.BaseRequest;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @description:
 * @author: wyk
 * @time: 2021/10/9 15:49
 */
public class ClassWorkAdapter extends BaseExpandableListAdapter {

    /**
     * 添加类型：全部
     */
    public static final int ADD_TYPE_ALL = 0;

    /**
     * 添加类型：已添加
     */
    public static final int ADD_TYPE_YES = 1;

    /**
     * 添加类型：未添加
     */
    public static final int ADD_TYPE_NO = 2;

    private int clientId;

    private long completeDate;

    /**
     * 添加类型
     */
    private int addType;

    private NoScrollListView publish_work_child;

    private List<ClassWorkRecord> pageList;

    private Context mContext;

    private TimePickerView pvTime;

    private ClassWorkListChildAdapter dkWorkListChildAdapter;

    public ClassWorkAdapter(Context context, List<ClassWorkRecord> classWorkRecords) {
        this.mContext = context;
        this.pageList = classWorkRecords;
    }

    @Override
    public int getGroupCount() {
        return pageList == null ? 0 : pageList.size();
    }

    @Override
    public int getChildrenCount(int i) {
        return 1;
    }

    @Override
    public Object getGroup(int i) {
        return pageList == null ? 0 : pageList.get(i);
    }

    @Override
    public Object getChild(int i, int i1) {
        return 0 == pageList.get(i).getEworkcontents().size() ? null : pageList.get(i).getEworkcontents().get(i1);
    }

    @Override
    public long getGroupId(int i) {
        return i;
    }

    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
        GroupViewHolder groupViewHolder = null;
        if (null == view) {
            view = LayoutInflater.from(mContext).inflate(R.layout.activity_class_work_parent, null);
            groupViewHolder = new GroupViewHolder();
            groupViewHolder.publish_work_date = view.findViewById(R.id.publish_work_date);
            groupViewHolder.publish_work_member = view.findViewById(R.id.publish_work_member);
            groupViewHolder.publish_work_name = view.findViewById(R.id.publish_work_name);
            groupViewHolder.work_type_image = view.findViewById(R.id.work_type_image);
            groupViewHolder.tea_work_alarm_clock = view.findViewById(R.id.tea_work_alarm_clock);
            view.setTag(groupViewHolder);
        } else {
            groupViewHolder = (GroupViewHolder) view.getTag();
        }
        setGroupView(i, groupViewHolder);
        return view;
    }

    @Override
    public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
        if (null == view) {
            view = LayoutInflater.from(mContext).inflate(R.layout.activity_class_work_child, null);
        }

        publish_work_child = (NoScrollListView) view;
        List<ClassWorkContent> workList = pageList.get(i).getEworkcontents();
        dkWorkListChildAdapter = new ClassWorkListChildAdapter(mContext,pageList.get(i), workList);
        publish_work_child.setAdapter(dkWorkListChildAdapter);
        return view;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }

    private void setGroupView(final int i, final GroupViewHolder groupViewHolder) {
        final ClassWorkRecord publishWorkRecords = pageList.get(i);
        if (null != publishWorkRecords) {
            long publishDate = publishWorkRecords.getPublishDate() * 1000;
            groupViewHolder.publish_work_date.setText(CWUtils.formatDate3(publishDate));
            groupViewHolder.work_type_image.setImageResource(R.drawable.class_work);
            groupViewHolder.publish_work_name.setText(publishWorkRecords.getWorkName());
            groupViewHolder.publish_work_member.setText("作业成员: " + publishWorkRecords.getReviceObject());
        }
    }

    /**
     * 清空数据
     */
    public void clear() {
        if (pageList != null) {
            pageList.clear();
            //notifyDataSetChanged();
        }
    }

    public void addAll(List<ClassWorkRecord> pageList) {
        if (this.pageList == null) {
            this.pageList = new ArrayList<ClassWorkRecord>();
        }
        if (pageList != null) {
            switch (addType) {
                case ADD_TYPE_ALL:
                    this.pageList.addAll(pageList);
                    break;

                case ADD_TYPE_YES:
                    for (ClassWorkRecord publishWorkRecords : pageList) {
//                        if (epaperInfo.isAdded()) {
                        this.pageList.add(publishWorkRecords);
//                        }
                    }
                    break;

                default:
                    for (ClassWorkRecord publishWorkRecords : pageList) {
//                        if (epaperInfo.isAdded()) {
                        this.pageList.add(publishWorkRecords);
//                        }
                    }
                    break;
            }
            notifyDataSetChanged();
        }
    }

    /**
     * 组视图助手
     */
    private class GroupViewHolder {
        /**
         * 作业日期
         */
        private TextView publish_work_date;

        /**
         * 作业类型
         */
        private ImageView work_type_image;

        /**
         * 作业名
         */
        private TextView publish_work_name;

        /**
         * 作业成员
         */
        private TextView publish_work_member;



        private ImageView tea_work_alarm_clock;

    }

}
